/**
 * PwdValidator.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2015年5月22日
 */
package com.pshare.framework.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;

import com.pshare.constant.ReturnMsg;
import com.pshare.constant.SeaxConstants;
import com.pshare.framework.annotation.LoginNameCheck;

/**
 * 密码格式验证
 */
public class loginNameValidator implements ConstraintValidator<LoginNameCheck, CharSequence> {

	private static String REGEX = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

	private static Pattern pattern = Pattern.compile(REGEX);
	@Override
	public void initialize(LoginNameCheck constraintAnnotation) {

	}

	@Override
	public boolean isValid(CharSequence object, ConstraintValidatorContext constraintContext) {
		if(StringUtils.isBlank(object))
			return false;
		String input = object.toString();
		boolean valid = true;
		Matcher matcher = pattern.matcher(input);
		if (StringUtils.isBlank(input)|| !matcher.matches()) {
			valid = false;
		}

        if(!valid) {
            constraintContext.disableDefaultConstraintViolation();
            constraintContext.buildConstraintViolationWithTemplate(ReturnMsg.WRONG_FORMAT_LONG_NAME).addConstraintViolation();
        }
        
        return valid;
	}

}
